package com.localjoycommunity.cpdj.project.system.controller;

import com.localjoycommunity.cpdj.common.utils.poi.ExcelUtil;
import com.localjoycommunity.cpdj.framework.aspectj.lang.annotation.Log;
import com.localjoycommunity.cpdj.framework.aspectj.lang.enums.BusinessType;
import com.localjoycommunity.cpdj.framework.web.controller.BaseController;
import com.localjoycommunity.cpdj.framework.web.domain.AjaxResult;
import com.localjoycommunity.cpdj.framework.web.page.TableDataInfo;
import com.localjoycommunity.cpdj.project.system.domain.SysOption;
import com.localjoycommunity.cpdj.project.system.domain.SysQuestion;
import com.localjoycommunity.cpdj.project.system.service.ISysOptionService;
import com.localjoycommunity.cpdj.project.system.service.ISysQuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 试题Controller
 *
 * @author leepandar
 * @date 2020-11-01
 */
@RestController
@RequestMapping("/system/question")
public class SysQuestionController extends BaseController {
    @Autowired
    private ISysQuestionService sysQuestionService;

    @Autowired
    private ISysOptionService sysOptionService;

    /**
     * 查询试题列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SysQuestion sysQuestion) {
        startPage();
        List<SysQuestion> list = sysQuestionService.selectSysQuestionList(sysQuestion);
        return getDataTable(list);
    }

    /**
     * 导出试题列表
     */
    @Log(title = "试题", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SysQuestion sysQuestion) {
        List<SysQuestion> list = sysQuestionService.selectSysQuestionList(sysQuestion);
        ExcelUtil<SysQuestion> util = new ExcelUtil<SysQuestion>(SysQuestion.class);
        return util.exportExcel(list, "question");
    }

    /**
     * 获取试题详细信息
     */
    @GetMapping(value = "/{questionId}")
    public AjaxResult getInfo(@PathVariable("questionId") Long questionId) {
        SysQuestion sysQuestion = sysQuestionService.selectSysQuestionById(questionId);
        List<SysOption> sysOptions = sysOptionService.selectSysOptionListByQuestionId(questionId);
        sysQuestion.setOptions(sysOptions);
        return AjaxResult.success(sysQuestion);
    }

    /**
     * 新增试题
     */
    @Log(title = "试题", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysQuestion sysQuestion) {
        int i = sysQuestionService.insertSysQuestion(sysQuestion);
        return toAjax(i);
    }

    /**
     * 修改试题
     */
    @Log(title = "试题", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysQuestion sysQuestion) {
        // 1. 更新question
        int i = sysQuestionService.updateSysQuestion(sysQuestion);
        // 2. 更新 option
        List<SysOption> options = sysQuestion.getOptions();
        options.forEach(item -> {
            sysOptionService.updateSysOption(item);
        });
        return toAjax(i);
    }

    /**
     * 删除试题
     */
    @Log(title = "试题", businessType = BusinessType.DELETE)
    @DeleteMapping("/{questionIds}")
    public AjaxResult remove(@PathVariable Long[] questionIds) {
        return toAjax(sysQuestionService.deleteSysQuestionByIds(questionIds));
    }
}
